package com.h3bpm.web.organization.service.impl;

import com.h3bpm.base.util.AppUtility;
import com.h3bpm.web.common.utils.SpringContextUtil;
import com.h3bpm.web.organization.service.OrganizationSyncService;
import org.quartz.Job;
import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author
 * @date2019/8/30
 */
public class OrgSyncJob implements Job  {
    private static final Logger LOGGER = LoggerFactory.getLogger(OrgSyncJob.class);


    @Override
    public void execute(JobExecutionContext content) throws JobExecutionException {
        System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())+ "★★★★★★★★★★★");
        OrganizationSyncService organizationSyncService = (OrganizationSyncService) SpringContextUtil.getApplicationContext().getBean(OrganizationSyncServiceImpl.class);
        organizationSyncService.syncDepartmentService();
        organizationSyncService.syncUserService();
        organizationSyncService.syncUserManager();
        try {

            AppUtility.getEngine().getOrganization().Reload();

        } catch (Exception e) {
            e.printStackTrace();
        }
        String jobName = content.getJobDetail().getKey().getName();
        JobDataMap dataMap = content.getJobDetail().getJobDataMap();
        String param = dataMap.getString("param");
        System.out.println("传递的参数是="+param +"任务名字是="+jobName);
    }






    public static String formatDateByPattern(Date date,String dateFormat){
        SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
        String formatTimeStr = null;
        if (date != null) {
            formatTimeStr = sdf.format(date);
        }
        return formatTimeStr;
    }
    public static String getCron(Date  date){
        String dateFormat="ss mm HH dd MM ? yyyy";
        return formatDateByPattern(date, dateFormat);
    }


}
